package com.example.resourceSharingPlatform.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.resourceSharingPlatform.entity.Transaction;
import com.example.resourceSharingPlatform.service.TransactionService;

@RestController
public class TransactionController {
	
	@Autowired
    private TransactionService transactionService;

	/*
	 * 查询订单信息
	 * 1. localhost:8080/transactionByID?id=
	 *    根据订单编号查询订单信息
	 *    
	 * 2. localhost:8080/transactionByLender_id?lender_id=
	 *    根据借出者id查询订单信息
	 *    
	 * 3. localhost:8080/transactionByBorrow_id?borrow_id=
	 *    根据借入出者id查询订单信息
	 *    
	 * 4. localhost:8080/transactionByItem_id?item_id=
	 *    根据商品编号查询订单信息
	 */
    @GetMapping("/transaction/byID")
    Transaction getUserById(@RequestParam(value = "id")int id){
        return transactionService.queryTransactionById(id);
    }
    
    @GetMapping("/transaction/byLender_id")
    List<Transaction> queryTransactionByLender_id(@RequestParam(value = "lender_id")String lender_id){
        return transactionService.queryTransactionByLender_id(lender_id);
    }
    
    @GetMapping("/transaction/byBorrow_id")
    List<Transaction> queryTransactionByBorrow_id(@RequestParam(value = "borrow_id")String borrow_id){
        return transactionService.queryTransactionByBorrow_id(borrow_id);
    }
    
    @GetMapping("/transaction/byItem_id")
    List<Transaction> queryTransactionByItem_id(@RequestParam(value = "item_id")int item_id){
        return transactionService.queryTransactionByItem_id(item_id);
    }
    
    /*
     * 申请交易的插入
     * 1.localhost:8080/applyTrans
     */
    @PostMapping("/applyTrans")
    public Transaction applyTransaction(@RequestBody Transaction transaction){
        return transactionService.insertTransaction(transaction);
    }

    /**
     *更新交易状态
     * -1代表交易被拒绝，0代表交易待确认，1代表交易进行中， 2代表交易已完成
     */
    @PutMapping("/updateTransState")
    public Boolean agreeTrans(@RequestParam(value="id")int id, @RequestParam(value="state")String state){
        return transactionService.updateTransState(id, state);
    }

    @PostMapping("/updateTransState")
    public Boolean greeTrans(@RequestParam(value="id")int id, @RequestParam(value="state")String state){
        return transactionService.updateTransState(id, state);
    }
    /*
     * 删除语句
     * 1.localhost:8080/transaction?id=
     */
    @DeleteMapping("/transaction")
    public boolean deleteTransactionById(@RequestParam(value = "id") int id){
    	return transactionService.deleteTransactionById(id);
    }


    /*
     * 更新语句
     * 1.根据订单编号更新，目前只需要更新交易状态 
     * 1.localhost:8080/transaction?
     */
    @PutMapping("/transaction")
    boolean updateTransaction(@RequestBody Transaction transaction){
        return transactionService.updateTransaction(transaction);
    }

}
